﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenSSL.X509;
using OpenSSL.Core;
using Sicurezza.Client.Manager;
using OpenSSL.Crypto;
using System.Threading;
using Sicurezza.Client.Controlli.CA;
using Sicurezza.Entita;

namespace Sicurezza.Client.Controlli
{
    public partial class Form1 : Form
    {
        
        private SendFileControl sfc = new SendFileControl();
        private RequestControl rc = new RequestControl();
        private CertificateControl cc = new CertificateControl();
        private LoadFileControl lfc = new LoadFileControl();
        


        public Form1()
        {
            InitializeComponent();
            sfc.Visible = false;
            rc.Visible = false;
            cc.Visible = false;
            lfc.Visible = false;
            ControlPanel.Controls.Add(sfc);
            ControlPanel.Controls.Add(rc);
            ControlPanel.Controls.Add(cc);
            ControlPanel.Controls.Add(lfc);


            rc.GeneratingKeys += new EventHandler(rc_GeneratingKeys);
            rc.GeneratingKeysEnd += new EventHandler(rc_EndGeneratingKeys);
            rc.SendingRequest += new EventHandler(rc_SendingRequest);
            rc.SendingRequestEnd += new EventHandler(rc_SendingRequestEnd);
            rc.RequestAccepted += new EventHandler(rc_CertificateArrived);
            rc.RequestSaved += new EventHandler(rc_RichiestaSalvata);
            rc.RenewAccepted += new EventHandler(rc_RenewAccepted);
            rc.RequestEnd += new EventHandler(rc_RequestEnd);

            cc.OpeningRenew += new EventHandler(cc_OpeningRenew);

            sfc.RequestingRecipients += new EventHandler(sfc_RequestingRecipients);
            sfc.RequestingRecipientsEnd += new EventHandler(sfc_RequestingrecipientsEnd);
            sfc.SendingMail += new EventHandler(sfc_SendingMail);
            sfc.SendingMailEnd += new EventHandler(sfc_SendingMailEnd);
        }

        void sfc_SendingMailEnd(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "Ready";
            Refresh(); ;
        }

        void sfc_SendingMail(object sender, EventArgs e)
        {
            string recipient = sender as string;
            toolStripStatusLabel1.Text = "Sending mail to " + recipient + "...";
            Refresh();
        }

        void sfc_RequestingrecipientsEnd(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "Ready";
            Refresh();
        }

        void sfc_RequestingRecipients(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "Receiving recipients from CA...";
            Refresh();
        }

        void rc_RequestEnd(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "Ready";
            Refresh();
        }

        void rc_RenewAccepted(object sender, EventArgs e)
        {
            string cert = sender as string;
            if (cert != null)
            {
               Reset();
               cc.Visible = true;
               cc.OpenString(cert);
            }

        }

        void cc_OpeningRenew(object sender, EventArgs e)
        {
            string RenId = sender as string;
            if (!string.IsNullOrEmpty(RenId))
            {
                Reset();
                rc.Visible = true;
                rc.OpenRenew(RenId);
            }
        }

        void rc_RichiestaSalvata(object sender, EventArgs e)
        {
            MessageBox.Show("Request saved",
                    "Request saved",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information);



            toolStripStatusLabel1.Text = "Ready";
            Refresh();
        }

        void rc_CertificateArrived(object sender, EventArgs e)
        {
            string cert = sender as string;
            if (!string.IsNullOrEmpty(cert))
            {
                Reset();
                cc.Visible = true;
                cc.OpenString(cert);
            }
        }

        void rc_SendingRequestEnd(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "Request sent";
            Refresh();
        }

        void rc_SendingRequest(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "Sending request to CA...";
            Refresh();
        }

        void rc_EndGeneratingKeys(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "Ready";
            Refresh();
        }

        void rc_GeneratingKeys(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "Generating keys...";
            Refresh();
        }

        

        private void requestToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Reset();
            ChooseKeyLenght ckl = new ChooseKeyLenght();
            if (ckl.ShowDialog() != DialogResult.OK || ckl.KeyLenght == 0)
                return;
            rc.GenerateKeys(ckl.KeyLenght);
            rc.Visible = true;
        }

        private void Reset()
        {
            sfc.Visible = false;
            cc.Visible = false;
            rc.Visible = false;
            lfc.Visible = false;

            sfc.Reset();
            cc.Reset();
            rc.Reset();
            lfc.Reset();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void requestToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            OpenForm or = new OpenForm(OpenForm.Mode.Request);
            if (or.ShowDialog() == DialogResult.OK && !string.IsNullOrEmpty(or.SelectedId))
            {
                Reset();
                string ReqId = or.SelectedId;
                string cert = "";
                try
                {
                    cert = RequestManager.Check(ReqId);
                }
                catch (ManagerException ex)
                {
                    MsgBox.Rejected(ex.Message);
                    Reset();
                    return;
                }
                if (string.IsNullOrEmpty(cert))
                {
                    rc.Visible = true;
                    rc.Open(ReqId);
                }
                else
                {
                    MsgBox.VerifyOk();
                    cc.Visible = true;
                    cc.OpenString(cert);
                }
            }
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            cc.SaveCertificate();
        }

        private void certificateToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
        }       

        private void renewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenForm or = new OpenForm(OpenForm.Mode.Renew);
            if (or.ShowDialog() == DialogResult.OK && !string.IsNullOrEmpty(or.SelectedId))
            {
                Reset();
                rc.Visible = true;
                rc.OpenRenew(or.SelectedId);
            }
        }

        private void fromFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() != DialogResult.OK || string.IsNullOrEmpty(openFileDialog1.FileName))
                return;

            cc.Visible = true;
            cc.OpenFile(openFileDialog1.FileName);

        }

        private void fromRepositoryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenForm or = new OpenForm(OpenForm.Mode.Certificate);
            if (or.ShowDialog() == DialogResult.OK && !string.IsNullOrEmpty(or.SelectedId))
            {
                Reset();
                cc.Visible = true;
                cc.Open(or.SelectedId);
            }
        }

        private void sendFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Reset();
            sfc.Visible = true;
        }

        private void loadEncipherSignedMessageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Reset();
            lfc.Visible = true;
        }
    }
}
